﻿namespace OboutInc.EasyMenu_Pro
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Reflection;

    [Serializable, ToolboxItem(false)]
    public class MenuItemBaseCollection : CollectionBase
    {
        private CSSClassesCollection _classes;

        public MenuItemBaseCollection(CSSClassesCollection classes)
        {
            this._classes = classes;
        }

        public int Add(ItemBase menuComponent)
        {
            if (!base.InnerList.Contains(menuComponent))
            {
                this._classes.Add(new CSSClasses(menuComponent.GetType()));
                return base.InnerList.Add(menuComponent);
            }
            return -1;
        }

        public int AddAt(ItemBase menuComponent, int index)
        {
            if (!base.InnerList.Contains(menuComponent))
            {
                this._classes.Add(new CSSClasses(menuComponent.GetType()));
                ArrayList list = (ArrayList) base.InnerList.Clone();
                base.InnerList.Clear();
                base.InnerList.AddRange(list.GetRange(0, index));
                int num = base.InnerList.Add(menuComponent);
                base.InnerList.AddRange(list.GetRange(index, list.Count - index));
                return num;
            }
            return -1;
        }

        public bool Contains(ItemBase menuComponent)
        {
            if (menuComponent.GetType().Name.Equals("MenuItem"))
            {
                return base.InnerList.Contains((MenuItem) menuComponent);
            }
            return (menuComponent.GetType().Name.Equals("MenuSeparator") && base.InnerList.Contains((MenuSeparator) menuComponent));
        }

        public int firstIndexOfType(string type)
        {
            for (int i = 0; i < base.List.Count; i++)
            {
                if (base.List[i].GetType().FullName == type)
                {
                    return i;
                }
            }
            return -1;
        }

        public void Remove(ItemBase menuComponent)
        {
            base.InnerList.Remove(menuComponent);
            this.removeUnusedClasses();
        }

        public void Remove(int index)
        {
            base.InnerList.RemoveAt(index);
            this.removeUnusedClasses();
        }

        public void removeUnusedClasses()
        {
            int index = 0;
            while (index < this._classes.Count)
            {
                bool flag = false;
                for (int i = 0; i < base.Count; i++)
                {
                    if (this._classes[index].ObjectType == this[i].GetType().FullName)
                    {
                        flag = true;
                    }
                }
                if (!flag)
                {
                    this._classes.RemoveAt(index);
                }
                else
                {
                    index++;
                }
            }
        }

        public ItemBase this[int index]
        {
            get
            {
                return (ItemBase) base.List[index];
            }
            set
            {
                base.List[index] = value;
            }
        }
    }
}

